import AdManage from "../manage/AdManage";
import VivoNativeAd from "../vivo/VivoNativeAd";
import config from "./config";

const func = {
    /**
     * 加载资源
     * @param path 路径 
     * @param type 资源类型
     * @param progressCallBack 加载进度回调 
     */
    loadResouce(path: string, type: typeof cc.Asset, progressCallBack?: (compeletedCount: number, totalCount: number, item: any) => void) {
        if (!path || !type) {
            console.log("参数错误", path, type);
            return;
        }
        return new Promise((reslove, reject) => {
            cc.resources.load(path, type, progressCallBack, (err, asset) => {
                if (err) {
                    cc.log("资源加载失败！", err);
                    reslove(null);
                    return;
                }
                reslove(asset);
            });
        });

    },

    /**
     * 打开弹窗
     * @param node 选择的节点
     * @param maxScale 最大缩放
     * @param minScale 正常缩放
     * @param callback 回调
     */
    openPopupAnimatation(node: cc.Node, maxScale: number = 1.1, minScale: number = 1, callback: Function = null) {
        node.active = true;
        node.scale = minScale / 2;
        node.opacity = 0;
        cc.tween(node)
            .to(0.07, { scale: maxScale, opacity: 255 })
            .to(0.02, { scale: minScale })
            .call(() => {
                callback && callback();
            })
            .start();
    },
    /**
     * 打开弹窗
     * @param node 选择的节点
     * @param maxScale 最大缩放
     * @param callback 回调
     */
    closePopupAnitation(node: cc.Node, maxScale: number = 1.1, callback: Function = null) {
        cc.tween(node)
            .to(0.02, { scale: maxScale })
            .to(0.1, { scale: 0 })
            .call(() => {
                callback && callback();
            })
            .start();
    },
    /**
      * 随机数
      * @param min 
      * @param max 
      */
    ranNum(min: number, max: number) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    },

    /**
     * 时间转换
     * @param num 
     */
    changeTime(maxtime: number) {
        if (maxtime <= 0) return "00:00";
        var minutes = Math.floor(maxtime / 60) < 10 ? `0${Math.floor(maxtime / 60)}` : Math.floor(maxtime / 60);
        var seconds = Math.floor(maxtime % 60) < 10 ? `0${Math.floor(maxtime % 60)}` : Math.floor(maxtime % 60);
        var msg = `${minutes}:${seconds}`;
        return msg;
    },
    /**
  * 弹窗动画
  * @param node 
  * @param callback 
  */
    setNodeAni(node: cc.Node, callback: Function = null, scaleTop: number = 1.1, scale: number = 1) {
        node.active = true;
        node.scale = 0.5;
        node.opacity = 0;
        let _sX = 1, _sY = 1;
        if (node) {
            _sX = cc.view.getVisibleSize().width / 720;
            _sY = cc.view.getVisibleSize().height / 1280;
        }
        cc.tween(node)
            .to(0.07, { scale: scaleTop, opacity: 255 })
            .to(0.02, { scale: scale })
            .call(() => {
                callback && callback();
            })
            .start()
    },
    //缩放式退出
    goReduceOut(node: cc.Node, callback: Function = null, scaleTop: number = 1.1, scale: number = 0) {
        cc.tween(node)
            .to(0.02, { scale: scaleTop })
            .to(0.1, { scale: scale })
            .call(() => {
                callback && callback();
            })
            .start()
    },
    //计算路线角度
    countAngle(start: cc.Vec3, end: cc.Vec3) {
        var dx = end.x - start.x;
        var dy = end.y - start.y;
        var dir = cc.v2(dx, dy);
        var angle = dir.signAngle(cc.v2(1, 0));
        //将弧度转换为欧拉角
        var degree = angle / Math.PI * 180;
        return degree;
    },
    creatrBanner() {
        if (config.showBanner){
            return
        }
        config.showBanner = true;
        let id = "";
        if (config.gameType == "vivo") {
            id = config.vivoBanner;
        }else if(config.gameType == "QQ"){
            id = config.qq_bannerAd;
        }
        AdManage.instance.initBannerAd(id);
    },
    destroyBanner() {
        config.showBanner = false;
        if (AdManage.instance.n_bannerAd) {
            AdManage.instance.n_bannerAd.destroy();
        }
    },
    /**
   * 创建vivo原生广告或插屏
   */
    async creatVivoNativeAd(bool: boolean = true) {
        if (config.gameType != "vivo" || config.qg_loadCfg == null) return;
        if (bool) {
            if (config.qg_loadCfg.zs_native_limit == 0 || (config.qg_loadCfg.zs_version != config.vivo_zs_version && config.qg_loadCfg.zs_native_limit == 1)) return;
        }
        config.openPopup = true;
        let view = cc.find('Canvas/view');
        let vivo = view.getChildByName("vivo");
        if (vivo && vivo.active) return;
        if (vivo) {
            vivo.active = true;
        } else {
            if (config.prefabArr[config.prefabPath.vivo]) {
                vivo = cc.instantiate(config.prefabArr[config.prefabPath.vivo]);
            } else {
                let prefab = await func.loadResouce(config.prefabPath.vivo, cc.Prefab) as cc.Prefab;
                config.prefabArr[config.prefabPath.vivo] = prefab;
                vivo = cc.instantiate(prefab);
            }
            vivo.parent = view;
        }
        vivo.zIndex = 10;
        vivo.getComponent(VivoNativeAd).init();
    },

}
export default func;